//LiuZh 2017-06-27
import React, { Component } from 'react';

import {
  View,
  Image,
  Dimensions,
  TouchableWithoutFeedback,
  StyleSheet,
  Text,
  TouchableOpacity,
  TextInput,
  FlatList,
  Alert
} from 'react-native';

console.disableYellowBox = true;



import KcjjItem from '../component/KcjjItem';
import FindTop  from '../component/FindTop';


//alert (A);


import  InitDB  from 'shequnsiweiapp/src/core/database/InitDB.js';
import  Http  from 'shequnsiweiapp/src/core/utils/Http.js';
//let { Loading, EasyLoading } = require('react-native-easy-loading');


//console.disableYellowBox = true

let isCheckOne = true;

let voices = [{ID_:-1,},];
let lmjj   = [{ID_:-1}]
let  course_user=new Object();

export default class FindScene extends Component{


    constructor(props) {
        super(props);

        this.state = {
            voices: voices,
        }
    }

    componentDidMount () {
         voices = [{ID_:-1,},];
         lmjj   = [{ID_:-1}]
         course_user=new Object();
         let dbVoices = InitDB.getVoices();
          voices =   voices.concat(dbVoices);
        this.setState({
            voices: voices,
        });

        realmObj = InitDB.getCourses();
        lmjj.push(realmObj[0]);

        realmObj = InitDB.getCourseUser('empId="'+InitDB.getCurrentUser().empid+'"');
        if(realmObj && realmObj.length>0){
           course_user=realmObj[0];
        }
        
        
    }


  render() {
    return(
      <FlatList data={this.state.voices}
        renderItem={this.flatListRender}
        initialNumToRender={4}/>
  )}; 

//listVIew渲染方法
  flatListRender = (data) => {
    return data.item.ID_ == -1 ? this.renderTop() : this.renderContent(data);
  }

topCallBack = (cheOne) => {
  isCheckOne = !isCheckOne;

  if (!cheOne) {
    this.setState({
      voices: voices,
    });
  } else {
    this.setState({
      voices: lmjj,
    });
  }
}

//渲染listView的头部
  renderTop = (item) => {
    return (
      <FindTop callBack={this.topCallBack}/>
  )}

// 渲染listView的item
  renderContent = (data) => {

        if(isCheckOne){
            return (<KcjjItem 
                data={data.item}
                onPress={this.pressItem.bind(this)}/>)
        } else {
          return (
            <View style={{backgroundColor: '#FFF',padding:10}}>
              <Text style={{color: '#000',marginBottom: 10}}>栏目简介:</Text>
              <Text >{data.item.COLUMN_DESC}</Text>

              <Text style={{color: '#000',marginBottom: 10,marginTop: 16}}>作者简介:</Text>
              <Text >{data.item.AUTHOR_DESC}</Text>

              <Text style={{color: '#000',marginBottom: 10,marginTop: 16}}>适宜人群:</Text>
              <Text >{data.item.SUITABLE_FOR}</Text>
            </View>
          )
        }

    
  }

 

  

  pressItem = (itemData) => {
   // alert(InitDB.getCourseUserCurrentUser()[0].ID_);
      let c = InitDB.getCourseUserCurrentUser() &&   InitDB.getCourseUserCurrentUser().length>0 
       &&  InitDB.getCourseUserCurrentUser()[0].ID_;
     if(itemData.FREE_==='0'   &&  !c ){
         alert('该音频为vip音频，必须购买专栏才能收听');

     }else{
    
    this.props.screenProps.appNavigation.navigate('Video', {
        ORDER_ :itemData.ORDER_,
    });
     }
  }

}


//appId